<template>
  <fml-dialog
    :addParam="addParam"
    :addApi="addApi"
    :editParam="editParam"
    :editApi="editApi"
    :dialogWidth="dialogWidth"
    :formConfig="formConfig"
    ref="editDialog"
    :dialogText="this.editData?'修改':'新增'"
    title="用户">
  </fml-dialog>
</template>
<script>
import { mapState } from "vuex";
import { defaultConfig } from "./dialog-config";
import { fmlRequest} from "../../../../utils";

export default {
  name: "addEditDialog",
  data() {
    return {
      organIds:[],
      formConfig: {
        tr:3,//联动索引
        td:0,//联动索引
        cascader:['cascader'],//联动ref
        defaultConfig,
        fmlForm: {
          roleIds:[]
        }
      },
      dialogWidth: "400px",
      addApi: 'roleList',
      editApi:'roleList',
      editor: null
    };
  },
  computed: {
    ...mapState(["dialogVisible", "editData",'dialogType',"inputFocus","inputName"]),
    initialFormValue() {
      return {
        roleIds:[],
        organRelIds:[]
      };
    },
    addParam() {
     let param={
        ...this.formConfig.fmlForm
      }
      param['roleIds']=this.formConfig.fmlForm.roleIds.join(',')

      return param;
    },
    editParam() {
      let param = {
        ...this.addParam
      };

      return param;
    }
  },
  methods:{
     // 获取机构
    getOrganizate(){
      fmlRequest.getOrganizate(this,0,false,1).then(result => {
        this.formConfig.defaultConfig.tr[1].td[0].treeData = result;
        this.$store.state.treeList=result
      });
    },
     //树形懒加载
    loadNode(node, resolve) {
      let that=this
      let id=node.data.id?node.data.id:0
      setTimeout(() => {
       fmlRequest.getOrganizate(this,id,false).then(result => {
          resolve(result);
          let orid=this.formConfig.fmlForm.organRelIds
          this.formConfig.fmlForm.organRelIds=[]
          this.formConfig.fmlForm.organRelIds=orid
        });
      }, 500);
    }
  },
  mounted(){
    this.getOrganizate()
  },
  watch: {
    dialogVisible: {
      immediate: true,
      handler(v) {
        // 初始化和每次关闭弹窗是重置表单数据
        if (!v) {
          this.formConfig.fmlForm = { ...this.initialFormValue };
          this.formConfig.defaultConfig.tr[1].td[0].treeData=[]

        }else{
          // this.formConfig.defaultConfig.tr[1].td[0].loadNode=this.loadNode
          this.formConfig.defaultConfig.tr[1].td[0].treeData=this.$store.state.treeList
          this.$nextTick(()=>{
            let formConfig=this.formConfig.defaultConfig.tr
            formConfig[0].td[0].selectList=this.$store.state.roleList
          })
        }
      }
    },
    editData(v) {
      console.log(v)
      let fmlForm = this.formConfig.fmlForm;
      var formConfig=this.formConfig.defaultConfig.tr;
      if (v && Object.keys(v).length) {
        this.formConfig.defaultConfig.treeText.organRelIds=v.organRelNames
        const roleIds=v.roleIds?v.roleIds.split(','):[]
        this.organIds=v.organIds?v.organIds.split(','):[]
        const organIds=v.organIds?[v.organIds.split(',').map(Number)[0]]:[]
        const {
          organRelIds,
          id,
        } = v;
        // 不能直接将数据赋值给addEditForm，这会导致表单传入的form不再是原来的对象，从而使表单的全局方法，如resetFields失效
        Object.assign(fmlForm, {
          organRelIds,
          roleIds,
          id
        });
      }
    }
  }
};
</script>
